//
//  LSCustomView.m
//  CROSBY
//
//  Created by Harvey on 2018/3/27.
//  Copyright © 2018年 Haley. All rights reserved.
//

#import "LSGraffitiView.h"
#import "LSSquareView.h"

@interface LSGraffitiView()<UIGestureRecognizerDelegate>

@property (strong, nonatomic) NSMutableArray            *squareViews;

@end

@implementation LSGraffitiView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        _rows = kGraffiRows;
        _cols = kGraffiCols;
        [self initSubView];
        [self initData];
    }
    return self;
}

- (void)initData
{
    NSMutableArray *data = [NSMutableArray array];
    for (int i = 0; i < self.cols; i++) {
        NSMutableArray *colsArray = [[NSMutableArray alloc] init];
        for (int j = 0; j < self.rows; j++) {
            [colsArray addObject:@(NO)];
        }
        [data addObject:colsArray];
    }
    _data = [data copy];
}

- (NSMutableArray *)squareViews
{
    if (!_squareViews) {
        _squareViews = [[NSMutableArray alloc] init];
    }
    return _squareViews;
}

- (void)initSubView
{
    for (int i = 0; i < self.cols; i++) {
        NSMutableArray *colsArray = [[NSMutableArray alloc] init];
        for (int j = 0; j < self.rows; j++) {
            LSSquareView *view = [[LSSquareView alloc] initWithFrame:CGRectZero];
            view.indexPath = [NSIndexPath indexPathForRow:j inSection:i];
            UITapGestureRecognizer *tapGesture = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapClick:)];
            [view addGestureRecognizer:tapGesture];
            [self addSubview:view];
            [colsArray addObject:view];
        }
        [self.squareViews addObject:colsArray];
    }
    
    UIPanGestureRecognizer *panGesture = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(panAction:)];
    panGesture.delegate = self;
    [self addGestureRecognizer:panGesture];
}

- (void)setData:(NSArray *)data
{
    if (!data || data.count == 0) {
        return;
    }
    _data = data;
    
    for (int i = 0; i < data.count; i++) {
        NSArray *array = [data objectAtIndex:i];
        for (int j = 0; j < array.count; j++) {
            NSNumber *number = [array objectAtIndex:j];
            LSSquareView *view = self.squareViews[i][j];
            view.isSelected = number.boolValue;
        }
    }
}

- (void)reset
{
    for (int i = 0; i < self.data.count; i++) {
        NSArray *array = [self.data objectAtIndex:i];
        for (int j = 0; j < array.count; j++) {
            LSSquareView *view = self.squareViews[i][j];
            view.isSelected = NO;
            self.data[i][j] = @(NO);
        }
    }
}

- (void)tapClick:(UITapGestureRecognizer *)gesture
{
    LSSquareView *view = (LSSquareView *)gesture.view;
    if (_delegate && [_delegate respondsToSelector:@selector(graffitiView:touchOn:)]) {
        [_delegate graffitiView:self touchOn:view];
    }
}

- (void)panAction:(UIPanGestureRecognizer *)gesture
{
    CGPoint point = [gesture locationInView:self];
    
    LSSquareView *targetView = nil;
    for (int i = 0; i < self.data.count; i++) {
        NSArray *array = [self.data objectAtIndex:i];
        for (int j = 0; j < array.count; j++) {
            LSSquareView *view = self.squareViews[i][j];
            if (CGRectContainsPoint(view.frame, point)) {
                targetView = view;
                goto quit;
            }
        }
    }
    
    quit:{
        if (targetView && _delegate && [_delegate respondsToSelector:@selector(graffitiView:touchOn:)]) {
            [_delegate graffitiView:self touchOn:targetView];
        }
    }
}

- (void)layoutSubviews
{
    [super layoutSubviews];

    CGFloat sW = self.bounds.size.width / self.cols;
    CGFloat sH = sW;

    for (int i = 0; i < self.cols; i++) {
        for (int j = 0; j < self.rows; j++) {
            CGFloat sX = sW * i;
            CGFloat sY = sW * j;
            UIView *view = self.squareViews[i][j];
            view.layer.borderColor = [UIColor grayColor].CGColor;
            view.layer.borderWidth = 0.5;
            view.frame = CGRectMake(sX, sY, sW, sH);
        }
    }
}

- (BOOL)gestureRecognizer:(UIGestureRecognizer *)gestureRecognizer shouldRecognizeSimultaneouslyWithGestureRecognizer:(UIGestureRecognizer *)otherGestureRecognizer
{
    if ([otherGestureRecognizer.view isKindOfClass:[UIScrollView class]]) {
        return YES;
    }
    return NO;
}



@end
